<?php

class SiteController extends Controller
{
	public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform  actions
                'actions'=>array('ForgotPassword', 'ResetPassword', 'Login', 'Logout', 'Error'),
                'users'=>array('*'),
            ),  
            array('allow',   //allow authenticated user to perform actions
                'actions'=>array('index'),
                'users'=>array('@'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),            
        );
    }

	public function actionIndex()
	{
		echo 'This is admin panel.';
		$this->render('index');
	}

	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	public function actionLogin()
	{
		$model=new AdminLoginForm;

        if(isset($_POST['AdminLoginForm']))
	    {
            $model->attributes=$_POST['AdminLoginForm'];
            if($model->validate()){
                if (strpos(Yii::app()->user->returnUrl,'/index.php')===false)
                    $this->redirect(Yii::app()->user->returnUrl);
                switch (Yii::app()->user->role_id){
                    case ROLE_ADMIN:
                        $this->redirect(Yii::app()->createAbsoluteUrl('admin'));
                    break;

                    default :$this->redirect(Yii::app()->createAbsoluteUrl('admin'));
                }
            }
        }
        $this->render('login', array('model'=>$model));
	}

	public function actionLogout()
	{
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->createAbsoluteUrl('admin/site/login/'));
    }
}